<?php namespace App\Models\Cxhshop;

use App\Models\Base\CxhshopBase;

class AgentSubsidy extends CxhshopBase {

    protected $table = 'ims_cxh_apply_subsidy_agent';
    protected $fillable = [];
    public $timestamps = false;

    /**
     * 验证该账户是否申请了运营补贴
     * @param $menId
     * @param $type 2:判断该账号是否有代理商记录（无论是待审核还是拒绝还是同意）；1：之前的情况（其他）
     * @return array
     */
    public function checkAgentSubsidy($menId,$type = 1){
        $query = self::query();
        if($type == 1){
            $records = $query -> where('member_id',$menId) -> whereIn('apla_status',[0,1])-> get();
        }else{
            $records = $query -> where('member_id',$menId) -> get();
        }
        if($records){
            $data = $records -> toArray();
        }else{
            $data = [];
        }
        return $data;
    }

    /**
     * @param $memId
     * @param $uid
     * @param array $params
     * @return int
     */
    public function addAgentApply($uuid,$memId,$params = []){
        $data = [
            'apla_shop' => $params['apla_shop'],//店铺名
            'apla_shop_addr' => $params['apla_shop_addr'],//店铺地址
            'shop_pic' => $params['shop_pic'],//店铺图片
            'activity_pic' => $params['activity_pic'],//活动图片
            'member_id' => $memId,
            'member_uuid' => $uuid,
            'ctime' => time(),
            'utime' => time(),
        ];
        $query = self::query();
        $agentAplyId = $query -> insertGetId($data);
        return $agentAplyId;
    }

    /**
     *
     * @param $memId
     * @return array
     */
    public function getApplyDetail($memId){
        $query = self::query();
        $field = ['id','apla_shop','apla_shop_addr','apla_status','refuse_reason','is_all_back','ctime'];
        $list = $query -> where('member_id',$memId) -> orderBy('id','desc') -> first($field);
        if($list){
            $data = $list -> toArray();
        }else{
            $data = [];
        }
        return $data;

    }

    /**
     * 根据id及状态获取运营补贴记录
     * @param $id
     * @param $status
     * @return array
     */
    public function getAgentApplyById($id=0,$status=1,$fields=['*']){
        $record = self::query() -> where('id',$id) -> where('apla_status',$status) -> first($fields);
        if($record){
            $data = $record -> toArray();
        }else{
            $data = [];
        }
        return $data;
    }

    /**
     * 根据修改运营补贴相关字段
     * @param array $saveData
     * @param int $agentId
     * @return int
     */
    public function updateAgentFields($saveData = [],$agentId = 0){
        $res = self::query() -> where('id',$agentId) -> update($saveData);
        return $res;
    }
}